/*
 * FormHabitacion.java
 *
 * Created on 12/11/2010, 10:46:57 PM
 */
package pe.edu.upla.lamerced.desk.view.maestro;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.Utiles;
import pe.edu.upla.lamerced.bean.Categoria;
import pe.edu.upla.lamerced.bean.Habitacion;
import pe.edu.upla.lamerced.bean.Tipohabitacion;
import pe.edu.upla.lamerced.service.CategoriaService;
import pe.edu.upla.lamerced.service.HabitacionService;

/**
 *
 * @author elvis
 */
public class FormHabitacion extends javax.swing.JInternalFrame {

   private static final Logger logger = Logger.getLogger(FormHabitacion.class);
   private HabitacionService habitacionService;
   private CategoriaService categoriaService;
   private List<Categoria> listaCategorias;
   private List<Tipohabitacion> listaTipos;
   private List<String> listaEstados;
   private JDesktopPane escritorio;
   private Habitacion habitacion;
    /** Creates new form FormHabitacion */
    public FormHabitacion() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();
      jLabel4 = new javax.swing.JLabel();
      jLabel5 = new javax.swing.JLabel();
      cbCategoria = new javax.swing.JComboBox();
      cbTipoHabitacion = new javax.swing.JComboBox();
      txtNumero = new javax.swing.JTextField();
      cbEstado = new javax.swing.JComboBox();
      btnGrabar = new javax.swing.JButton();
      btncancelar = new javax.swing.JButton();
      txtPiso = new javax.swing.JTextField();
      jLabel6 = new javax.swing.JLabel();
      txtPrecio = new javax.swing.JTextField();

      setClosable(true);
      setTitle("Habitacion");

      jLabel1.setText("Categoria");

      jLabel2.setText("Tipo de habitacion");

      jLabel3.setText("Piso");

      jLabel4.setText("Numero");

      jLabel5.setText("Estado");

      cbCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

      cbTipoHabitacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

      cbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

      btnGrabar.setText("Grabar");
      btnGrabar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnGrabarActionPerformed(evt);
         }
      });

      btncancelar.setText("Cancelar");
      btncancelar.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btncancelarActionPerformed(evt);
         }
      });

      jLabel6.setText("Precio");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addGap(39, 39, 39)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                     .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                     .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING))
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(txtPrecio, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                     .addComponent(txtNumero, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtPiso)
                        .addComponent(cbEstado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cbCategoria, 0, 158, Short.MAX_VALUE)
                        .addComponent(cbTipoHabitacion, 0, 158, Short.MAX_VALUE))))
               .addGroup(layout.createSequentialGroup()
                  .addGap(59, 59, 59)
                  .addComponent(btnGrabar)
                  .addGap(59, 59, 59)
                  .addComponent(btncancelar)))
            .addGap(51, 51, 51))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(36, 36, 36)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jLabel2)
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(jLabel3)
                     .addComponent(txtPiso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
               .addComponent(cbTipoHabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jLabel4)
               .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(9, 9, 9)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(cbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(jLabel5))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel6)
               .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnGrabar)
               .addComponent(btncancelar))
            .addGap(31, 31, 31))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
       cargaListaForm();
       this.dispose();
    }//GEN-LAST:event_btncancelarActionPerformed

    private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
       try {
          if(cbCategoria.getSelectedIndex()==-1){JOptionPane.showMessageDialog(this, "Seleccione una categoria");return;}
          if(cbTipoHabitacion.getSelectedIndex()==-1){JOptionPane.showMessageDialog(this, "Seleccione un tipo de habitacion");return;}
          if( !Utiles.isNumber( txtPiso.getText() ) ){JOptionPane.showMessageDialog(this, " Piso No es un numero");return;}
          if( !Utiles.isNumber( txtNumero.getText() ) ){JOptionPane.showMessageDialog(this, "Numero de habitacion no es un numero");return;}
          if( !Utiles.isNumber( txtPrecio.getText() ) ){JOptionPane.showMessageDialog(this, "Precio no es un numero");return;}

          //Habitacion hab = new Habitacion();
          habitacion.setCategoria( listaCategorias.get(cbCategoria.getSelectedIndex()));
          habitacion.setTipohabitacion(listaTipos.get(cbTipoHabitacion.getSelectedIndex()));
          habitacion.setEstado(cbEstado.getSelectedIndex());
          habitacion.setPiso(Integer.parseInt(txtPiso.getText()));
          habitacion.setNumero(Integer.parseInt(txtNumero.getText()));
          habitacion.setPrecioBase(new BigDecimal(txtPrecio.getText()));
          habitacionService.registrarHabitacion(habitacion);
          JOptionPane.showMessageDialog(this, "Habitacion registrada");
          cargaListaForm();
          this.dispose();
       } catch (Exception e) {
          JOptionPane.showMessageDialog(this, e.getMessage(), "Error",JOptionPane.ERROR_MESSAGE);
          e.printStackTrace();
       }
    }//GEN-LAST:event_btnGrabarActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnGrabar;
   private javax.swing.JButton btncancelar;
   private javax.swing.JComboBox cbCategoria;
   private javax.swing.JComboBox cbEstado;
   private javax.swing.JComboBox cbTipoHabitacion;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JLabel jLabel4;
   private javax.swing.JLabel jLabel5;
   private javax.swing.JLabel jLabel6;
   private javax.swing.JTextField txtNumero;
   private javax.swing.JTextField txtPiso;
   private javax.swing.JTextField txtPrecio;
   // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(habitacionService==null){habitacionService = (HabitacionService)Inicial.getFactory().getBean("habitacionService");}
      if(categoriaService==null){categoriaService = (CategoriaService)Inicial.getFactory().getBean("categoriaService");}

      listaCategorias = categoriaService.listarCategorias();
      cbCategoria.removeAllItems();
      logger.debug("lista d cat = " + listaCategorias.size()) ;
      for(Categoria cat : listaCategorias){
         cbCategoria.addItem(cat.getNombre());
      }
      listaTipos = habitacionService.listaTipoHabitacion();
      logger.debug("lista tipos "+listaTipos.size());
      cbTipoHabitacion.removeAllItems();
      for(Tipohabitacion th : listaTipos){
         cbTipoHabitacion.addItem(th.getNombre());
      }
      listaEstados = new ArrayList<String>();
      listaEstados.add("libre");
      listaEstados.add("reservada");
      listaEstados.add("hospedaje");
      listaEstados.add("mantenimiento");
      cbEstado.removeAllItems();
      for(String est : listaEstados){
         cbEstado.addItem(est);
      }

      txtNumero.setText( Utiles.nullToBlank( habitacion.getNumero() ));
      txtPiso.setText( Utiles.nullToBlank( habitacion.getPiso() ));
      txtPrecio.setText( Utiles.nullToBlank( habitacion.getPrecioBase() ));
      cbCategoria.setSelectedItem( Utiles.nullToBlank( habitacion.getCategoria().getNombre() ) );
      cbTipoHabitacion.setSelectedItem( Utiles.nullToBlank( habitacion.getTipohabitacion().getNombre() ) );
      cbEstado.setSelectedIndex( Utiles.nullToZero( habitacion.getEstado() ) );
   }

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }
   public Habitacion getHabitacion() {
      return habitacion;
   }
   public void setHabitacion(Habitacion habitacion) {
      this.habitacion = habitacion;
   }

   public void cargaListaForm(){
      ListaHabitaciones form = new ListaHabitaciones();
       form.setEscitorio(escritorio);
       form.cargaInicial();
       escritorio.add(form);
       form.setVisible(true);
   }
}
